const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const passport = require("passport");
const app = express();

// 引入 users.js
const users = require("./api/user");
const profiles = require("./api/profiles");

// DB mongoURI 连接
const db = require("./config/keys").mongoURI;

// 使用 body-parser 中间件
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

// 链接 db
mongoose.connect(db).then(() => {
    console.log(`mongoDB数据库连接成功!!!`);
}).catch(err => {
    console.log(err);
})

// 配置passport
// passport 初始化
app.use(passport.initialize());
require("./config/passport")(passport);

app.get("/", (req, res) => {
    res.send("helloword!");
})

// 使用 api
app.use("/api/users", users);
app.use("/api/profiles", profiles);

const port = process.env.PORT || 5000;
app.listen(port, () => {
    console.log(`当前post:${port}`);
})